package com.wuyou.mid;

import javax.lang.model.type.IntersectionType;
import java.util.*;

/**
 * 面试题 01.08. 零矩阵
 */
public class SetZeroes {


    public void solution_2(int[][] matrix) {
        Map<Integer, Integer> map = new HashMap<>();
        List<Integer> jList = new ArrayList<>();
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (jList.contains(j)) {
                    matrix[i][j] = 0;
                }
                if (matrix[i][j] == 0) {
                    map.put(i, j);
                    if (!jList.contains(j)) {
                        jList.add(j);
                    }
                }
            }
        }
        Set<Integer> integers = map.keySet();
        for (Integer i : integers) {
            for (int j = 0; j < matrix[i].length; j++) {
                matrix[i][j] = 0;
            }
            Integer j = map.get(i);
            for (int k = 0; k < i; k++) {

                matrix[i][j] = 0;
            }
        }
    }

    public void solution_1(int[][] matrix) {

        List<Integer> iList = new ArrayList<>();
        List<Integer> jList = new ArrayList<>();
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] == 0) {
                    jList.add(j);
                    iList.add(i);
                }
            }
        }
        for (int i = 0; i < matrix.length; i++) {
            if (iList.contains(i)) {
                for (int j = 0; j < matrix[i].length; j++) {
                    matrix[i][j] = 0;
//                    Arrays.fill(matrix[i], 0);
                }
            }
        }
//        iList.forEach(item -> {
//            Arrays.fill(matrix[item], 0);
//        });
        for (int i = 0; i < jList.size(); i++) {

            for (Integer j : iList) {
                matrix[iList.get(j)][jList.get(i)] = 0;
            }
        }

    }
}
